﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Job.Data.Interfaces;
using Job.Logic.Common;
using Job.Entity;
using Job.Web.Framework;

namespace Job.Logic.BaseContent
{
    public class HunterJobLogic
    {
        private readonly IJobHunterDao dao = DataAccess.jobHunterDao;
        private readonly ICommon commonDao = DataAccess.commonDao;

        private const string BACK_LIST_SQL = @"Select job_id,job_no,name,industry,salary,office_address,create_date From job_hunter";

        //不能存在相同JobNo的记录
        public bool Insert(JobHunter bean)
        {
            IList<JobHunter> list = dao.FindByJobNo(bean.JobNo);
            if (list == null || list.Count == 0)
            {
                dao.Insert(bean);
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool Update(JobHunter bean)
        {
            dao.Update(bean);
            return true;
        }

        public void DeleteById(int jobId)
        {
            JobHunter bean = new JobHunter();
            bean.JobId = jobId;
            dao.Delete(bean);
        }
        public void DeleteByNo(string jobNo)
        {
            dao.DeleteByJobNo(jobNo);
        }


        public JobHunter GetDetail(int jobId)
        {
            return dao.Find(jobId);
        }
        public JobHunter GetDetail(String jobNo)
        {
            IList<JobHunter> list = dao.FindByJobNo(jobNo);
            if (null == list || list.Count == 0) return null;
            return list[0];
        }

        public int GetCount() 
        {
            CommonLogic common = new CommonLogic();
            int count = common.GetCountBySql(BACK_LIST_SQL);
            return count;
        }

        //private string GetWhere(string key)
        //{
        //    key = (null == key) ? "" : key.Trim();
        //    if (key.Equals("")) return " 1=1 ";

        //    key = key.Replace("'","''");
        //    StringBuilder buf = new StringBuilder();
        //    buf.Append("      name like '%");
        //    buf.Append(key);
        //    buf.Append("%' Or industry like '%");
        //    buf.Append(key);
        //    buf.Append("%' Or office_address like '%");
        //    buf.Append(key);
        //    buf.Append("%' Or responsibility like '%");
        //    buf.Append(key);
        //    buf.Append("%' Or grade like '%");
        //    buf.Append(key);
        //    buf.Append("%'");
        //    return buf.ToString();
        //}

        public int GetCount(JobHunter jobSearcher)
        {
            CommonLogic common = new CommonLogic();
            string sql = "Select * From job_hunter Where " + GetWhere(jobSearcher);
            int count = common.GetCountBySql(sql);
            return count;
        }

        public DataTable GetJobPageList(int pageSize, int pageIndex)
        {
            CommonLogic common = new CommonLogic();
            DataSet ds = common.GetPageDataBySql(BACK_LIST_SQL, pageSize, pageIndex, 0,
                "create_date",SortDirection.Descending,null);
            return ds.Tables[0];
        }
        public DataTable GetJobPageList(int pageSize, int pageIndex,JobHunter jobSearcher)
        {
            CommonLogic common = new CommonLogic();
            string sql = BACK_LIST_SQL + " Where " + GetWhere(jobSearcher);
            DataSet ds = common.GetPageDataBySql(sql, pageSize, pageIndex, 0,
                "create_date", SortDirection.Descending, null);
            return ds.Tables[0];
        }


        public DataTable GetTopJob(int number)
        {
            return GetJobPageList(number, 1);
        }


        private string GetWhere(JobHunter jobSearcher)
        {
            StringBuilder buf = new StringBuilder();
            buf.Append(" 1=1 ");
            if (jobSearcher.Name != null && jobSearcher.Name.Length > 0)
            {
                buf.Append(" And name like '%");
                buf.Append(jobSearcher.Name.Replace("'", "''"));
                buf.Append("%' ");
            }
            if (jobSearcher.SearchKey != null && jobSearcher.SearchKey.Length > 0)
            {
                buf.Append(" And (responsibility like '%");
                buf.Append(jobSearcher.SearchKey.Replace("'", "''"));
                buf.Append("%' Or grade like '%");
                buf.Append(jobSearcher.SearchKey.Replace("'", "''"));
                buf.Append("%') ");
            }
            //薪资水平预定义在数据库表中，按照从低到高的编号保存
            if (jobSearcher.Salary != null && jobSearcher.Salary.Length > 0 
                && !jobSearcher.Salary.Equals("0"))
            {
                buf.Append(" And salary>='" + jobSearcher.Salary + "' ");
            }

            if (jobSearcher.Industry != null && jobSearcher.Industry.Length >= 4)
            {
                buf.Append(" And industry='" + jobSearcher.Industry + "' ");
            }
            else if (jobSearcher.Industry != null && jobSearcher.Industry.Length > 1)
            {
                buf.Append(" And industry like '" + jobSearcher.Industry + "%' ");
            }

            if (jobSearcher.OfficeAddress != null && jobSearcher.OfficeAddress.Length >= 4)
            {
                buf.Append(" And office_address = '" + jobSearcher.OfficeAddress + "' ");
            }
            if (jobSearcher.OfficeAddress != null && jobSearcher.OfficeAddress.Length >= 1)
            {
                buf.Append(" And office_address like '" + jobSearcher.OfficeAddress + "%' ");
            }

            return buf.ToString();

        }
    }
}
